<template>
  <div class="MyArticle">
    <div class="descriptions-container">
      <el-card>
        <span v-if="articleList.length == 0 && !(article.searchContent == ''||article.searchContent == null)"style="display: flex;justify-content: center">
          <p style="padding-right: 100px;color: #ccc;">未找到对应文章</p>
        </span>
        <span v-if="articleList.length == 0 && (article.searchContent == ''||article.searchContent == null)" style="display: flex;justify-content: center">
          <p style="padding-right: 100px;">您还没有发布过文章...</p>
          <el-button type="text" @click="$router.push('/user/postArticle')">立即前往发布</el-button>
        </span>
        <div v-for="(item, index) in articleList" :key="index">
          <!-- 文章内容区域 -->
          <div style="display: flex;">
            <!-- 封面图片区域 -->
            <div style=" width: auto;margin-right: 20px; display: flex;justify-content: center;align-items: center;">
              <img v-if="item.picture" :src="item.picture" class="avatar">
            </div>
            <!-- 文字区域 -->
            <div style=" width: auto;">
              <el-descriptions :colon="false" direction="vertical" :column="1">
                <el-descriptions-item>
                  <p @click="$router.push('/articleDetail?articleID=' + item.id + '&authorID=' + item.authorID)"
                    style="margin: 5px 0; font-weight: bolder;font-size: 18px; cursor: pointer;">
                    {{ index + 1 }}、 {{ item.title }}
                  </p>
                </el-descriptions-item>
                <el-descriptions-item>
                  <p @click="$router.push('/articleDetail?articleID=' + item.id + '&authorID=' + item.authorID)"
                    style="margin: 5px 0;padding-bottom: 14px; font-size: 12px; width: auto;cursor: pointer;">
                    {{ item.content }}
                  </p>
                </el-descriptions-item>
                <el-descriptions-item>
                  <p style="margin: 0;font-size: small;">
                    <i style="margin-right: 50px;color: #409EFF;">{{ item.category_name }}</i>
                    <i @click="$router.push('/user/postArticle?articleId=' + item.id)"
                      style="margin-right: 20px;cursor: pointer;" class="el-icon-edit"> 编辑</i>
                    <i @click="DeleteArticle(item.id)" style="margin-right: 3px;cursor: pointer;"
                      class="el-icon-delete"> 删除</i>
                  </p>
                </el-descriptions-item>
              </el-descriptions>
            </div>

          </div>
          <!-- 分割线 -->
          <el-divider></el-divider>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getMyArticleAPI, deleteMyArticleAPI } from '@/api/myArticle'
export default {
  data() {
    return {
      article: {
        searchContent: ''
      },
      articleList: [],
    }
  },
  mounted() {
    this.article.searchContent = this.$route.query.searchContent
    this.getArticleList()
  },
  methods: {
    // 删除文章
    DeleteArticle(id) {
      this.$confirm("你确定要删除吗？").then(async () => {
        console.log(id)
        const res = await deleteMyArticleAPI(id)
        this.getArticleList()
        if (res.code == 200) {
          this.$message.success("删除成功")
          this.article.pageNum = 1
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => {
        this.$message.info("已取消")
      })
    },
    // 获取文章列表
    async getArticleList() {
      const res = await getMyArticleAPI(this.article)
      if (res.code == 200) {
        this.articleList = res.data.rows
      }
    },
  }
}
</script>

<style>
.MyArticle {
  padding: 10px;

  .avatar {
    width: 90px;
    height: 90px;
    border-radius: 8px;
  }

  .descriptions-container {

    .el-descriptions :not(.is-bordered) .el-descriptions-item__cell {
      padding-bottom: 0px;
    }

    .el-descriptions__body .el-descriptions__table .el-descriptions-item__cell {
      max-width: 200px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }

    

    .el-divider--horizontal {
      margin: 12px 0;
    }

  }
}
</style>
